/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-07 16:50:50
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-18 09:23:31
 */
import gameCtr from "../ctrs/gameCtr";
import guideCtr from "../ctrs/guide/guideCtr";
import { GuideElement } from "../ctrs/guide/GuideElement";
import BaseGameLayer from "../libs/core/BaseGameLayer";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { addButtonEvent, removeButtonEvent, seekNodeWithName2 } from "../libs/core/utils";
import { SceneManager } from "../libs/manager/SceneManager";
import { INotification } from "../libs/pureMvc/Interfaces";


const { ccclass, property } = cc._decorator;

@ccclass
export class guideLayer extends BaseGameLayer {

  @property(cc.Node)
  private tipCirShine: cc.Node = null;

  @property(cc.Node)
  private maskTop: cc.Node = null;

  @property(cc.Node)
  private maskRight: cc.Node = null;

  @property(cc.Node)
  private maskBottom: cc.Node = null;

  @property(cc.Node)
  private maskLeft: cc.Node = null;

  @property(cc.Label)
  private txtWord: cc.Label = null;

  @property(cc.Node)
  private imgWordBottom: cc.Node = null;

  @property(cc.Node)
  private imgTipsNewFishReward: cc.Node = null;

  @property(cc.Node)
  private imgPoint: cc.Node = null;

  @property(cc.Node)
  private background: cc.Node = null;

  @property(cc.Node)
  private btnSkill: cc.Node = null;

  private _buttonList: Array<cc.Button> = [];

  public onLoad(): void {

  }

  public onEnable() {
    super.onEnable();
    this.setGuideIndex();
  }

  public onDisable(): void {

    for (let k = 0; k < this._buttonList.length; ++k) {
      this._buttonList[k].interactable = true;
    }
    super.onDisable();
  }

  /** pureMVC 消息监听列表 */
  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_TASK_STATE_HAS_CHANGED,];
  }

  /**
   * pureMVC消息监听处理函数
   * @notification 消息体
   */
  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    let body = notification.getBody();

    switch (evtName) {

      case PureMVCEvents.ON_TASK_STATE_HAS_CHANGED:
        {

          this.jumpToCurrTaskNextGuideIndex();

        }
        break;
    }
  }

  private onTouchTargetTodoNextTaskGuideListener(evt: cc.Event): void {

    let target: cc.Node = evt.currentTarget;
    let btnTarget: cc.Button = target.getComponent(cc.Button);

    this.maskTop.x = 0;
    this.maskTop.y = 0;
    this.maskTop.width = 2000;
    this.maskTop.height = 2000;

    // btnTarget.interactable = false;

    this._buttonList.push(btnTarget);

    this.showGuideViews(false);
    this.jumpToCurrTaskNextGuideIndex();

    if (this.node) {

      removeButtonEvent(btnTarget, "onTouchTargetTodoNextTaskGuideListener", this.node, "guideLayer");
    }
  }

  private updateTipsRewardView(): void {

  }

  public onTouchStageListener(evt: cc.Event): void {
    // TipsTool.getInstance().createGeneralTips("先完成引导！");
  }


  private jumpToCurrTaskNextGuideIndex(): void {

    guideCtr.ins.setNextGuideIndex();

    this.setGuideIndex();
  }

  private setGuideIndex() {
    if (guideCtr.ins.guideIndex) {

      this.shineTarget();

    } else {

      SceneManager.getInstance().removeLayer(this.mediator.getMediatorName());
    }
  }


  private showGuideViews(isShow: boolean): void {
    this.background.active = !isShow;
    this.imgPoint.active = isShow;
    this.imgWordBottom.active = isShow;
    this.tipCirShine.active = isShow;

    if (!isShow) {
      this.maskTop.x = 0;
      this.maskTop.y = 0;
      this.maskTop.width = 2000;
      this.maskTop.height = 2000;
    }
  }


  private shineTarget(): void {
    this.maskTop.x = 0;
    this.maskTop.y = 0;
    this.maskTop.width = 2000;
    this.maskTop.height = 2000;

    let that: guideLayer = this;
    let vo: GuideElement = guideCtr.ins.getCurrentGuide();
    let layer: BaseGameLayer = null;

    this.node.stopActionByTag(9997);
    let actGetLayer: cc.ActionInterval = cc.repeatForever(cc.sequence(cc.delayTime(0.1), cc.callFunc(function () {
      layer = SceneManager.getInstance().getLayer(vo.layer);

      if (layer) {

        // setTimeout(function () {
        let vo: GuideElement = guideCtr.ins.getCurrentGuide();
        that.shineTargetStep(layer, vo);
        that.updateTipsRewardView();
        // }, 200);
        that.node.stopActionByTag(9997);
      } else {
        // setTimeout(function () {
        let vo: GuideElement = guideCtr.ins.getCurrentGuide();
        that.shineTargetStep(gameCtr.ins.curScene, vo);
        that.updateTipsRewardView();
        // }, 200);
        that.node.stopActionByTag(9997);
      }
    })));
    actGetLayer.setTag(9997);
    this.node.runAction(actGetLayer);
  }

  private shineTargetStep(layer: any, vo: any): void {
    let that: any = this;
    let parent: cc.Node = null;

    let func: Function = function () {
      parent = layer.node || layer;
      // let target: cc.Node = Utils.seekNodeWithName(vo.rectNode, parent);
      // console.log(parent)
      let target: cc.Node = seekNodeWithName2(vo.path, parent);

      let btnTarget: cc.Button = target.getComponent(cc.Button);

      if (target) {
        if (btnTarget && !vo.condition) {
          addButtonEvent(btnTarget, "onTouchTargetTodoNextTaskGuideListener", that.node, "guideLayer", "btnTarget");
        }
        that.updateShineTarget(target, parent, vo);
        that.background.active = false;
        that.tipCirShine.active = vo.whetherForce;
        if (btnTarget) {
          btnTarget.interactable = true;
        }
        return true;
      }
      return false;
    };

    if (!func()) {
      let act: cc.ActionInterval = cc.repeatForever(cc.sequence(cc.delayTime(0.3), cc.callFunc(func)));
      act.setTag(10011);
      this.node.runAction(act);
    }
  }

  private updateShineTarget(target: cc.Node, parent: cc.Node, vo: any): void {
    this.showGuideViews(true);

    this.txtWord.string = vo.talk;

    // 先获取自身的位置
    const point = target.getPosition();
    // 再获取其所在的世界坐标系的位置 (这一步不正确)
    const wordPoint = target.parent.convertToWorldSpaceAR(point);
    // 转换到目标结点系的相应位置
    const resultPoint = parent.convertToNodeSpaceAR(wordPoint);
    resultPoint.x += vo.guidePosition.x;
    // resultPoint.y += vo.guidePosition.y;
    this.tipCirShine.setPosition(resultPoint);
    cc.log(point.toString(), resultPoint.toString());
    //  cc.log(target.name + ' L:::::L ' + parent.name+'  CC: ' + resultPoint.toString()  + JSON.stringify(vo));

    let size: cc.Size = target.getContentSize();
    // console.log(size)

    let topY: number = resultPoint.y + size.height / 2;
    let bottomY: number = resultPoint.y - size.height / 2;
    let rightLeftY: number = resultPoint.y;
    let rightX: number = resultPoint.x + size.width / 2;
    let leftX: number = resultPoint.x - size.width / 2;

    let topBottomWidth: number = size.width;
    let topBottomX: number = resultPoint.x;

    // console.log(topBottomX, topY, topBottomWidth)
    this.maskTop.x = topBottomX;
    this.maskTop.y = topY;
    this.maskTop.width = topBottomWidth;

    this.maskBottom.x = topBottomX;
    this.maskBottom.y = bottomY;
    this.maskBottom.width = topBottomWidth;

    this.maskRight.x = rightX;
    this.maskRight.y = rightLeftY;

    this.maskLeft.x = leftX;
    this.maskLeft.y = rightLeftY;

    this.maskTop.active = vo.must;
    this.maskBottom.active = vo.must;
    this.maskLeft.active = vo.must;
    this.maskRight.active = vo.must;

    this.txtWord.node.parent.x = resultPoint.x + parseInt(vo.talkPosition.x);
    this.txtWord.node.parent.y = resultPoint.y + parseInt(vo.talkPosition.y);

    this.imgPoint.x = resultPoint.x + parseInt(vo.arrowPosition.x);
    this.imgPoint.y = resultPoint.y + parseInt(vo.arrowPosition.y);
    this.imgPoint.angle = parseInt(vo.arrowRote);

    this.node.stopActionByTag(10011);

  }

}
